package com.dao.impl;

import com.bean.Building;
import com.dao.BuildingDAO;
import com.util.C3p0Utils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BuildingDAOImpl implements BuildingDAO {

    /**
     * BeanListHandler将结果列表封装
     * @return
     * @throws SQLException
     */
    @Override
    public List<Building> selectAll() throws SQLException {

        return C3p0Utils.qr.query("select * from buildingmsg", new BeanListHandler<Building>(Building.class));
    }
    /**
     * BeanHandler将单个列表封装
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public Building selectById(Integer id) throws SQLException {
        return C3p0Utils.qr.query("select * from buildingmsg  where buildId =?", new BeanHandler<>(Building.class), id);
    }
    @Override
    public boolean deleteById(Integer id) throws SQLException {
        return C3p0Utils.qr.update("delete from buildingmsg where buildId=?", id) > 0;
    }
    @Override
    public boolean deleteAll(String[] idsArr) throws SQLException {
        StringBuffer sql = new StringBuffer("delete from buildingmsg where buildId in (");
        for (int i = 0; i < idsArr.length; i++) {
            sql.append("?,");
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(")");
        return C3p0Utils.qr.update(sql.toString(), idsArr) > 0;
    }
    @Override
    public boolean insert(Building b) throws SQLException {
        return C3p0Utils.qr.update("insert into buildingmsg values(null,?,?,?,?,?,?,?)", b.getBuildName(),b.getBuildConstruct(),
                b.getBuildUnits(),b.getBuildFloors(),b.getFloorRooms(),b.getBuildDesc(),b.getBuildTime()) > 0;
    }
    @Override
    public boolean update(Building b) throws SQLException {
        return C3p0Utils.qr.update("update buildingmsg set buildName=?,buildConstruct=?,buildUnits=?,buildFloors=?,floorRooms=?,buildDesc=?,buildTime=? where buildId=?",
                b.getBuildName(),b.getBuildConstruct(),b.getBuildUnits(),b.getBuildFloors(),b.getFloorRooms(),b.getBuildDesc(),b.getBuildTime(),b.getBuildId()) > 0;
    }



    /**
     * ScalarHandler用来将单个Object类型封装
     * @return
     * @throws SQLException
     */
    @Override
    public Integer count() throws SQLException {
        int n = C3p0Utils.qr.query("select count(*) from buildingmsg", new ScalarHandler<Long>()).intValue();
        return n;
    }
    @Override
    public Integer countSearch(String search) throws SQLException {
        int n = C3p0Utils.qr.query("select count(*) from buildingmsg where concat(buildName,buildConstruct,buildUnits,buildFloors,floorRooms,buildDesc,buildTime) like ?", new ScalarHandler<Long>(),"%"+search+"%").intValue();
        return n;
    }
    @Override
    public List<Building> selectAllPagination(Integer offset, Integer limit) throws SQLException {
        return C3p0Utils.qr.query("select * from buildingmsg limit ?,?", new BeanListHandler<>(Building.class), offset, limit);

    }
    @Override
    public List<Building> selectAllPaginationSearch(Integer offset, Integer limit, String search) throws SQLException {
        return C3p0Utils.qr.query("select * from buildingmsg where concat(buildName,buildConstruct,buildUnits,buildFloors,floorRooms,buildDesc,buildTime) like ? limit ?,?",
                new BeanListHandler<>(Building.class),"%"+search+"%",offset,limit);

    }

}
